/**
 * 
 */
package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DictionaryApplication;
import com.dhcc.DRGsHosp.core.domain.Dictionary;
import com.dhcc.DRGsHosp.infra.util.MonitorResultUtil;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

/**
 * @author chl
 *
 */
@Named
@Transactional
public class DictionaryApplicationImpl implements DictionaryApplication {
	private QueryChannelService queryChannel;
	
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	
	private EntityRepository getRepository() {
		EntityRepository repository = null;
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
	private QueryChannelService getQueryChannelService(){
	       if(queryChannel==null){
	          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
	       }
	     return queryChannel;
	    }

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryApplication#getDictionaryById(java.lang.Integer)
	 */
	@Override
	public Dictionary getDictionaryById(Integer id) {
		// TODO Auto-generated method stub
		return Dictionary.get(Dictionary.class, id);
	}


	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryApplication#removeDictionary(com.dhcc.DRGsHosp.core.domain.Dictionary)
	 */
	@Override
	public void removeDictionary(Dictionary dictionary) {
		dictionary.remove();
	}


	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryApplication#findAllDictionary()
	 */
	@Override
	public List<Dictionary> findAllDictionary() {
		// TODO Auto-generated method stub
		StringBuilder jpql = new StringBuilder() ;
		jpql.append("select d from Dictionary d order by d.id desc ") ;
		List<Dictionary> list = getQueryChannelService().createJpqlQuery(jpql.toString()).list() ;
		return list;
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryApplication#getDictionaryBydicCode(java.lang.String)
	 */
	@Override
	public Dictionary getDictionaryBydicCode(String dicCode) {
		StringBuilder jpql = new StringBuilder() ;
		jpql.append("select d from Dictionary d where d.dicCode = '" + dicCode + "'") ;
		Dictionary dictionary = (Dictionary) getQueryChannelService().createJpqlQuery(jpql.toString()).list().get(0) ;
		return dictionary;
	}

	/**
	 * 
	 *  @desp  ：保存字典
	 *  @Author： lhj
	 *  @param json
	 *  @Date  :2018年2月6日下午5:30:07
	 */
	@Override
	public void saveDictionary(Map<String, Object> json) {
		String dicCode = json.get("dicCode")!=null?json.get("dicCode").toString():"";
		String dicDesc = json.get("dicDesc")!=null?json.get("dicDesc").toString():"";
		Dictionary dic= new Dictionary();
		dic.setDicCode(dicCode);
		dic.setDicDesc(dicDesc);
		dic.save();
	}
	
	@Override
	public List<Object[]> findDictoryByParam(Map<String, Object> map){
		String dicCode = MonitorResultUtil.object2String(map.get("dicCode"));
		String dicdDesc = MonitorResultUtil.object2String(map.get("dicdDesc"));
		String dicValue = MonitorResultUtil.object2String(map.get("dicValue"));
		String dicDesc = MonitorResultUtil.object2String(map.get("dicValue"));
		String sql = " select a.dic_code, a.dic_desc, b.dic_value, b.dic_desc dicd_desc from drgs.MR_DICTIONARY a left join drgs.MR_DICTIONARYDATA b on a.dic_code=b.dic_code where 1=1 \n ";
		if(StringUtils.hasLength(dicCode)){
			sql += " and a.dic_code='"+dicCode+"'";
		}
		if(StringUtils.hasLength(dicdDesc)){
			sql += " and b.dic_desc='"+dicdDesc+"'";
		}
		if(StringUtils.hasLength(dicDesc)){
			sql += " and a.dic_desc='"+dicDesc+"'";
		}
		if(StringUtils.hasLength(dicValue)){
			sql += " and b.dic_value='"+dicValue+"'";
		}
		return getRepository().createSqlQuery(sql).list();
	}
	/**
	 * 
	 *  @Description：删除关联数据
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.DictionaryApplication#removeDictionaryData(java.lang.String)
	 */
	@Override
	public void removeDictionaryData(String id) {
		String sql="delete drgs.mr_dictionarydata a where a.dic_code='"+id+"'";
		jdbcTemplateObj.execute(sql);
		
	}
	/**
	 *  @Description：获取字典明细
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.DictionaryApplication#getDictionaryDtlBydicCode(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getDictionaryDataByCode(String dicCode) {
		return jdbcTemplateObj.queryForList("select a.dic_code ,a.dic_value ,a.dic_desc,a.dic_resume from drgs.mr_dictionarydata a where a.dic_code='"+dicCode+"' ");
	}
	/**
	 *  @Description：获取字典
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.DictionaryApplication#findDictionaryByCode(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> findDictionaryByCode(String dicCode) {
		return jdbcTemplateObj.queryForList("select t.dic_code,t.dic_desc from drgs.MR_DICTIONARY t where t.dic_code='"+dicCode+"' ");

	}
}
